<?php ob_start();?>
<?php 
	$root = $_SERVER['DOCUMENT_ROOT'];
//  	include($root.'/standart/head.php');
 	include($root.'/standart/dateHeader.php');
 	include($root.'/standart/dbconnect.php');
 	$db = connect();

 	session_start();
if(isset($_SESSION['id'])){	
	$userID = $_SESSION['id'];
	$formData = $_POST['formData'];
	$jsonData = $_POST['jsonData'];
	
	$lastOrderQuery = "SELECT MAX(Order_Id) FROM ORDER_ITEMS";
	$lastOrderQueryResult = $db->query($lastOrderQuery);
	while($queryRecord = $lastOrderQueryResult->fetch_assoc()){
		//Son orderID alınarak yeni bir order id olusturuluyor
		$lastOrderId = $queryRecord['MAX(Order_Id)'] + 1;
	}
	echo $lastOrderId;
	
	$resultJS = json_decode($jsonData, true);

	$resultFD = json_decode($formData, true);
	
	//Form Data'nın islenmesi
	$addressId = $resultFD['addressId'];
	$orderDate = $resultFD['orderDate'].':00';
	$totalAmount = $resultFD['totalAmount'];
	$orderNotes = $resultFD['orderNotes'];
	
	//JSON data'nin islenmesi
	class Order {
		public $menuId = "";
		public $orderId  = "";
		public $price = "";
		public $quantity = "";
		public $itemNote = "";
	}
	
	$orderList = array();
	foreach ($resultJS as $key => $item) {
		$order = new Order();
		$order->menuId = $item['menuId'];
		$order->orderId = $lastOrderId;
		$order->price = $item['price'];
		$order->quantity = $item['quantity'];
// 		print_r($order);
		array_push($orderList, $order);
	}
	
	foreach ($orderList as $item) {
		$menuId = $item->menuId;
		$price = $item->price;
		$quantity = $item->quantity;
		
	}
	/*
	echo "<br />";
	echo  "<pre>";
	print_r($orderDate);
	echo "</pre>";
	echo "<br />";
	
	echo  "<pre>";
	    print_r($_POST);
	echo "</pre>"; 
	echo "<br />";
	echo  "<pre>";
	print_r($addressId);
	print_r($orderDate);
	print_r($totalAmount);
	echo "</pre>";
	echo "<br />";
	echo  "<pre>";
	print_r($resultFD);
	echo "</pre>";
	echo "<br />";
	echo  "<pre>";
	print_r($lastOrderId);
	echo "</pre>";
	*/
	//Save Operation - ORDERS Table
	$stmt ="INSERT INTO ORDERS (USER_ID, ORDER_DATE, TOTAL_AMOUNT, SHIPPING_ADDRESS, NOTES)
						VALUES(?,?,?,?,?)";
	
	$statement = $db->prepare($stmt);
	$statement->bind_param('isdis', $userID, $orderDate, $totalAmount, $addressId, $orderNotes);
	
	if($statement->execute()){
		
		$orderItem_Id = $db->insert_id;
		$statement->close();
			foreach ($orderList as $item) {
				$menuId = $item->menuId;
				$price = $item->price;
				$quantity = $item->quantity;		
	
				$stmtOrderItem ="INSERT INTO ORDER_ITEMS (MENU_ID, ORDER_ID, PRICE, QUANTITY)
							VALUES(?,?,?,?)";
				$stmtAdd = $db->prepare($stmtOrderItem);
				$stmtAdd->bind_param('iidi', $menuId, $orderItem_Id, $price, $quantity);
				
				if($stmtAdd->execute()){
					$stmtAdd->close();
				}
				else{
					echo "Order_Items tablosuna insert hatası!";
				}
			} // Foreach sonu
	}
	else{
		echo $statement->error;
		echo "Orders Tablosuna insert hatası!";
	}
	
	
}
?>
<?php ob_flush(); ?>